package com.red.service.impl;

import com.red.mapper.HouseMapper;
import com.red.pojo.Result;
import com.red.pojo.model.LoginUser;
import com.red.service.IHousePushService;
import com.red.util.HttpStatusUtil;
import com.red.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class HousePushService implements IHousePushService {

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public Result push(int userId, int size) {
        List<Map<String, Object>> houses = houseMapper.pushHouse(userId);
        List<Map<String, Object>> pushHouses = new ArrayList<>();

        if (size > houses.size()) {
            size = houses.size();
        }

        ArrayList<Integer> randomSeq = new ArrayList<>();

        randomSeq.add((int) (Math.random() * houses.size()));

        // 拿到 size 个随机但不重的数
        while (randomSeq.size() < size) {
            int r = (int) (Math.random() * houses.size());

            try{
                for (Integer integer : randomSeq) {
                    if (integer == r) {
                        throw new Exception();
                    }
                }
            }catch (Exception e){
                continue;
            }
            randomSeq.add(r);
        }

        System.out.println(randomSeq);

        for (int r : randomSeq) {
            pushHouses.add(houses.get(r));
        }

        return new Result(HttpStatusUtil.SUCCESS, "获取成功", pushHouses);
    }
}
